﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Order.Domain.Entity
{
    /// <summary>
    /// 订单
    /// 订单:id,订单编号，订单总价，用户id，创建时间，
    /// </summary>
    public class Order : BaseAggregationRoot
    {

        public Order()
        {

        }

        public Order(string orderUniqueCode, string customerId, List<OrderItem> orderItemList)
        {
            OrderUniqueCode = orderUniqueCode;
            OrderStatus = OrderStatusEnum.OrderGenerated;
            CustomerId = customerId;
            CreateTime = DateTime.Now;
            Status = true;
            var itemList = new List<OrderItem>();
            foreach (var item in orderItemList)
            {
                var orderItem = OrderItem.AddOrderItem(this.Id, item.SkuPrice, item.SkuQuantity, item.SkuId, item.OrderProduct?.SpuName);
                itemList.Add(orderItem);
            }
            OrderItemList = itemList;
            TotalPrice = itemList.Sum(e => e.SkuQuantity * e.SkuPrice);
        }

        public static Order CreateOrder(string orderUniqueCode, string customerId, List<OrderItem> orderItemList) 
        {
            var order = new Order(orderUniqueCode, customerId, orderItemList);
            return order;
        }

        public void SetStatus() 
        {
            this.Status = false;
        }

        /// <summary>
        /// 订单唯一编号
        /// </summary>
        public string OrderUniqueCode { get; private set; }

        /// <summary>
        /// 总价
        /// </summary>
        public decimal TotalPrice { get; private set; }

        /// <summary>
        /// 订单状态
        /// </summary>
        public OrderStatusEnum OrderStatus { get; private set; }

        /// <summary>
        /// 客户Id
        /// </summary>
        public string CustomerId { get; private set; }

        /// <summary>
        /// 订单创建时间
        /// </summary>
        public DateTime CreateTime { get; private set; }

        /// <summary>
        /// 订单状态
        /// </summary>
        public bool Status { get; private set; }

        /// <summary>
        /// 订单明细
        /// </summary>
        public virtual List<OrderItem> OrderItemList { get; private set; }
    }

    /// <summary>
    /// 订单状态
    /// </summary>
    public enum OrderStatusEnum
    {
        /// <summary>
        /// 已生成
        /// </summary>
        OrderGenerated = 0,
        /// <summary>
        /// 已支付成功
        /// </summary>
        OrderSuccessfullyPaid = 1,
        /// <summary>
        /// 已支付失败
        /// </summary>
        OrderPaymentFailed = 2,
        /// <summary>
        /// 未支付
        /// </summary>
        OrderUnPayment = 3
    }
}
